//
//  ComposeViewController.swift
//  WBSwift
//
//  Created by 贺瑞 on 2017/8/11.
//  Copyright © 2017年 herui. All rights reserved.
//

import UIKit

class ComposeViewController: UIViewController {
    
    @IBOutlet weak var textV: HHPlaceholderTextView!
    @IBOutlet weak var consB_textV: NSLayoutConstraint!
    @IBOutlet weak var picSelectorV: ComposePicSelectorView!
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        textV.becomeFirstResponder()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNoti()
        picSelectorV.presentedVC = self
        
    }
    
    deinit {
        HHLog("ComposeViewController - deinit")
    }
}
extension ComposeViewController {
    
 
    func setupNoti() {
        //键盘通知
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil, queue: OperationQueue.main) { [weak self] (noti) in
            
            let begin = noti.userInfo?[UIKeyboardFrameBeginUserInfoKey] as! CGRect
            let end = noti.userInfo?[UIKeyboardFrameEndUserInfoKey] as! CGRect
            
            if(begin.origin.y > end.origin.y){
                self?.consB_textV.constant = begin.size.height
            }else{
                self?.consB_textV.constant = 0
            }
            
            let duration = noti.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
            
            UIView.animate(withDuration: duration, animations: { 
                self?.view.layoutIfNeeded()
            })
        }

    }
}

extension ComposeViewController {
    
    @IBAction func leftBarItemOnclick() {
        dismiss(animated: true, completion: nil)
    }
    @IBAction func rightBarItemOnclick() {
        dismiss(animated: true, completion: nil)
    }
    
    
    @IBAction func picBtnOnclick() {
        //选择图片
    }
    @IBAction func emojiBtnOnclick() {
        //表情
        textV.resignFirstResponder()
        
        let inputView = ComposeEmojiInputView()
        inputView.bounds = CGRect(x: 0, y: 0, width: 414, height: 300)
        textV.inputView = inputView
        textV.becomeFirstResponder()
        
        
    }

    @IBAction func keyboardBtnOnclick() {
        textV.resignFirstResponder()
        textV.inputView = nil
        textV.becomeFirstResponder()
    }
    
}

extension ComposeViewController : UITextViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        scrollView.resignFirstResponder()
    }
    
}
